package org.jeecg.modules.wlhy.fanyun.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.third.daikai.fanyun.dto.FyApiBaseResDTO;
import org.jeecg.modules.third.daikai.fanyun.dto.driver.FyDriverDTO;
import org.jeecg.modules.third.daikai.fanyun.dto.middleman.FyMiddlemanDTO;
import org.jeecg.modules.third.daikai.fanyun.dto.payee.FyPayeeDTO;
import org.jeecg.modules.third.daikai.fanyun.service.FanyunApiService;
import org.jeecg.modules.wlhy.fanyun.entity.HyFyDriver;
import org.jeecg.modules.wlhy.fanyun.mapper.HyFyDriverMapper;
import org.jeecg.modules.wlhy.fanyun.service.IHyFyDriverService;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriverAccountBook;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverAccountBookService;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hydriverbank.entity.HyDriverBank;
import org.jeecg.modules.wlhy.hydriverbank.service.IHyDriverBankService;
import org.jeecg.modules.wlhy.utils.BankTypeUtil;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: hy_fy_driver
 * @Author: duanlikao
 * @Date:   2023-02-08
 * @Version: V1.0
 */
@Service
public class HyFyDriverServiceImpl extends ServiceImpl<HyFyDriverMapper, HyFyDriver> implements IHyFyDriverService {

    @Resource
    private IHyDriverService hyDriverService;

    @Resource
    private FanyunApiService fanyunApiService ;

    @Resource
    private IHyDriverBankService hyDriverBankService;

    @Autowired
    private IHyDriverAccountBookService hyDriverAccountBookService;

    @Override
    public Result uploadDriver(String driverId, String domainURL) {

        HyDriver driver = hyDriverService.getById(driverId);
        if (driver == null) {
            throw new JeecgBootException("该司机上报信息不能为空");
        }

        HyFyDriver hyFyDriver = this.getById(driverId);
        if (hyFyDriver == null) {
            hyFyDriver = new HyFyDriver();
            hyFyDriver.setDriverId(driver.getId());
            hyFyDriver.setUploadTime(new Date());
        }

        //承运人
        if (!"1".equals(hyFyDriver.getMiddleman())) {
            FyMiddlemanDTO fyMiddlemanDTO = new FyMiddlemanDTO();
            fyMiddlemanDTO.setName(driver.getDriverName());
            fyMiddlemanDTO.setIdCard(driver.getIdcard());
            fyMiddlemanDTO.setIdCardAddress(driver.getAddress());
            fyMiddlemanDTO.setPhoneNumber(driver.getTelephone());
            fyMiddlemanDTO.setIdCardFrontUrl(domainURL + driver.getIdcardImg());
            fyMiddlemanDTO.setIdCardReverseUrl(domainURL + driver.getIdcardImgBack());
            String middlemaRes = fanyunApiService.middlemaPush(fyMiddlemanDTO);
            FyApiBaseResDTO r = JSONObject.parseObject(middlemaRes, FyApiBaseResDTO.class);
            if (CommonConstant.SC_OK_200.intValue() == r.getCode()) {
                hyFyDriver.setMiddleman(CommonConstant.HAS_SEND);
            }
        }

        //收款人
        if (!"1".equals(hyFyDriver.getPayee())) {
            FyPayeeDTO fyPayeeDTO = new FyPayeeDTO();
            fyPayeeDTO.setName(driver.getDriverName());
            fyPayeeDTO.setIdCard(driver.getIdcard());
            fyPayeeDTO.setPhoneNumber(driver.getTelephone());
            HyDriverBank hyDriverBank = this.hyDriverBankService.getDefaultDriverBank(driverId);
            fyPayeeDTO.setBankCardNumber(hyDriverBank.getBankNum());
            fyPayeeDTO.setBankName(BankTypeUtil.getBankNameByBankType(hyDriverBank.getBankType()));

            String payeeRes = fanyunApiService.payeePush(fyPayeeDTO);
            FyApiBaseResDTO fyApiBaseResDTO = JSONObject.parseObject(payeeRes, FyApiBaseResDTO.class);
            if (CommonConstant.SC_OK_200.intValue() == fyApiBaseResDTO.getCode()) {
                hyFyDriver.setPayee(CommonConstant.HAS_SEND);
            }
        }

        //司机信息
        FyDriverDTO paramDTO = this.driverTOFyDriverDTO(driver, domainURL) ;
        String res = fanyunApiService.driverPush( paramDTO);
        if (StrUtil.isEmpty(res)) {
            throw new JeecgBootException("该司机数据推送失败");
        }

        FyApiBaseResDTO r = JSONObject.parseObject(res , FyApiBaseResDTO.class);
        if (CommonConstant.SC_OK_200.intValue() != r.getCode()) {
            if (r.getMessage().contains("已存在")) {
                hyFyDriver.setUploadStatus("1");
                this.saveOrUpdateFyDriver(hyFyDriver);
                return Result.OK();
            } else {
                hyFyDriver.setUploadStatus("2");
                hyFyDriver.setUploadFailureReason(r.getMessage());
                this.saveOrUpdateFyDriver(hyFyDriver);
                return Result.error("推送失败,原因: 司机 " + driver.getDriverName() + r.getMessage());
            }
        }

        hyFyDriver.setUploadStatus("1");
        this.saveOrUpdateFyDriver(hyFyDriver);
        return Result.OK();
    }

//    private Result driverPush(HyDriver driver ,HyFyDriver hyFyDriver, String domainURL) {
//
//        FyDriverDTO paramDTO = this.driverTOFyDriverDTO(driver, domainURL) ;
//        String res = fanyunApiService.driverPush( paramDTO);
//
//        if(StringUtils.isBlank(res)){
//            //hyFyDriver.setUploadStatus("2");
//            //hyFyDriver.setUploadFailureReason("推送异常");
//            //this.saveOrUpdateFyDriver(hyFyDriver);
//            return Result.error("该司机推送异常");
//        }else{
//            FyApiBaseResDTO r = JSONObject.parseObject(res , FyApiBaseResDTO.class);
//            if ( 200 == r.getCode()) {
//                //hyFyDriver.setUploadStatus("1");
//                //this.saveOrUpdateFyDriver(hyFyDriver);
//                return Result.OK();
//            }else{
//                if(r.getMessage().contains("已存在")){
//                    //hyFyDriver.setUploadStatus("1");
//                    //this.saveOrUpdateFyDriver(hyFyDriver);
//                    return Result.OK();
//                }else{
//                    //hyFyDriver.setUploadStatus("2");
//                    //hyFyDriver.setUploadFailureReason(r.getMessage());
//                    //this.saveOrUpdateFyDriver(hyFyDriver);
//                    return Result.error("推送失败,原因: 司机 " + driver.getDriverName() + r.getMessage());
//                }
//            }
//        }
//    }

    public FyDriverDTO driverTOFyDriverDTO(HyDriver driver, String domianURL) {
        if (driver == null) {
            return null;
        }
        FyDriverDTO fyDriverDTO = new FyDriverDTO();

        fyDriverDTO.setName(driver.getDriverName());
        fyDriverDTO.setIdCard(driver.getIdcard());
        fyDriverDTO.setDriverLicenseNumber(driver.getDrivingLicense());
        fyDriverDTO.setIdCardAddress(driver.getAddress());
        fyDriverDTO.setDriverLicenseIssuingAuthority(driver.getIssuingOrganizations());
        fyDriverDTO.setQuasiDrivingModel(driver.getVehicleClass());
        fyDriverDTO.setPhoneNumber(driver.getTelephone());

        fyDriverDTO.setQualificationCertificateNumber(
             StringUtils.isNotEmpty(driver.getQualificationCertificate()) ?
                     driver.getQualificationCertificate() : driver.getIdcard()
        );
        fyDriverDTO.setDriverLicenseStartTime(driver.getValidPeriodFrom().getTime()/1000) ;
        fyDriverDTO.setDriverLicenseEndTime(driver.getValidPeriodTo().getTime()/1000);

        fyDriverDTO.setDriverLicenseUrl(domianURL + driver.getDriverImg());

        fyDriverDTO.setIdCardFrontUrl(domianURL + driver.getIdcardImg());
        fyDriverDTO.setIdCardBackUrl(domianURL +  driver.getIdcardImgBack());
        if(StringUtils.isNotBlank(driver.getQualificationImg())){
            List<String> qualificationCertificateUrl = new ArrayList<>() ;
            qualificationCertificateUrl.add(domianURL + driver.getQualificationImg());
            fyDriverDTO.setQualificationCertificateUrlList(qualificationCertificateUrl);
        }
        return fyDriverDTO;
    }


    @Override
    public boolean saveOrUpdateFyDriver(HyFyDriver hyFyDriver){
        try {
            hyFyDriver.setId(hyFyDriver.getDriverId());
            return this.saveOrUpdate(hyFyDriver);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }
}
